import { observable, action } from 'mobx';
import * as  Apis from '../apis/govern';
import { WeaLocaleProvider } from 'ecCom';
import { ModelStore } from './modelStore';
import { GovernReportStore } from './governReportStore';
import { GovernShareStore } from './governShareStore';
import { GovernMindStore } from './governMindStore';
import { GovernAnalysisStore } from './governAnalysisStore';
import { WeaForm } from 'comsMobx';
import { Modal, message } from 'antd'
import BtnType from '../util/btnType';
import TopStore from './topStore';
const getLabel = WeaLocaleProvider.getLabel;

export default class ProinfoStore {
    //@observable checkStore = new CheckMatterStore();
    @observable id;
    @observable type;
    @observable status = {
        loading: false,
        maininfoTitle: "",
        columns: { "0": { key: '0', title: getLabel('21223,22045', "督办详情") } },
        orderList: [],
        rightMenu: [],
        quickBtn: [],
        selectTabKey: "",
        sKeyFlag: {},//显示催办标识
        isdelete: {},//是否删除
        tasksize1: 0,
        tasksize2: 0,
        roottask: false,
        superStatus: 0,//上级任务状态
    };
    @observable rightMenuCur = [];
    @observable skey = "";
    @observable MenuCurFlag = false;//当前右键事件是否来自内部区域点击（思维导图时用）
    @observable extensionFlag = false;//当前右键事件 延期（思维导图时用）
    @observable cutoffFlag = false;//当前右键事件 办结（思维导图时用）
    @observable loaded = false;
    @observable isRight;
    @observable shareLevel;
    @observable supervise1;
    @observable supervise2;
    @observable grStore = new GovernReportStore();//汇报store
    @observable gpStore = new GovernReportStore();//催办store
    @observable logStore = new GovernReportStore();//日志store
    @observable shareStore = new GovernShareStore();//共享store
    @observable governMindStore = new GovernMindStore();//思维导图store
    @observable governAnalysisStore = new GovernAnalysisStore();//数据统计store
    @observable docStore = new GovernReportStore();//成文store
    @observable giStore = new GovernReportStore();//领导点评store
    @observable geStore = new GovernReportStore();//延期store
    @observable gcStore = new GovernReportStore();//完结store

    @observable modelType = "";//弹出框加
    @observable visible = false;//弹出框加
    @observable modelStore = new ModelStore(this);//弹出框加
    @observable topStore = new TopStore(this);//弹出框加
    @observable cubeLayoutParame = {};//装载布局请求入参
    @observable Card = {};

    constructor(props) {
        window.showProModel = this.showProModel;
    }

    //弹出框加
    handleShareDialog = (bool) => {
        this.visible = bool;
    }

    @action
    changeTab = (key) => {
        this.rightMenuCur = [];
        this.MenuCurFlag = false;
        this.status.selectTabKey = key;
	    /* if(key == 'taskinfo'){
			this.getTaskForm({viewtype:'view',taskid:this.taskid});
	   } */
    }
    //获取·weatab的数组数据datas与右侧按钮组buttons;form数据
    @action
    getTitle(params, callback) {
        this.status.loading = true;
        this.cubeLayoutParame = {};
        this.id = params.id;
        this.type = params.type;
        Apis.getProInfo(params).then(
            action(data => {
                if (parseInt(data.status) == 1) {
                    if (data.isopen === true) {
                        this.cubeLayoutParame = data.data;
                    }
                    let { title = "", columns = [],orderList = [],  rightMenu = {}, quickBtn = {}, isRight, shareLevel, supervise1, supervise2, sKeyFlag, isdelete, conditions = [], otherParam = {} } = data;
                    this.loaded = true;
                    this.isRight = isRight;
                    this.shareLevel = shareLevel;
                    this.supervise1 = supervise1;
                    this.supervise2 = supervise2;
                    this.status.maininfoTitle = title;
                    this.status.columns = columns;
                    this.status.orderList = orderList;
                    //console.log("columns",columns);
                    this.status.rightMenu = rightMenu;
                    this.status.quickBtn = quickBtn;
                    if (params.selectTabKey) {
                        this.status.selectTabKey = params.selectTabKey;
                    } else {
                        this.status.selectTabKey = "0";
                    }
                    this.status.sKeyFlag = sKeyFlag;
                    this.status.isdelete = isdelete;
                    this.status.tasksize1 = conditions.length;
                    this.status.superStatus = _.has(otherParam, 'superStatus') ? otherParam.superStatus : 0;
                    if (callback && typeof callback === "function") {
                        callback(title);
                    }
                } else {
                    message.error(data.error);
                }
                this.status.loading = false;
            })).catch(error => {
                message.error(error);
            });
    }

    /**
     * 加载按钮
     */
    @action
    getCardBtnInfo(_this) {
        _this.status.loading = true;
        Apis.getCardBtnInfo({ id: _this.id }).then(data => {
            if (parseInt(data.status) == 1) {
                _this.status.rightMenu = data.rightMenu;
                _this.status.quickBtn = data.quickBtn;
            } else {
                message.error(data.error);
            }
            _this.status.loading = false;
        }).catch(error => {
            message.error(error);
        })
    }

    showProModel = (id, modeType) => {
        this.modelType = modeType;
        this.modelStore.showSpaModel(id, modeType);
    }

    /**
     * 搜索
     */
    onListSearch() {
        if (this.status.selectTabKey === "2") {//汇报
            this.grStore.doSearch();
        } else if (this.status.selectTabKey === "4") {//催办
            this.gpStore.doSearch();
        } else if (this.status.selectTabKey === "7") {//成文
            this.docStore.doSearch();
        } else if (this.status.selectTabKey === "8") {//领导批示
            this.giStore.doSearch();
        } else if (this.status.selectTabKey === "17") {//延期
            this.geStore.doSearch();
        } else if (this.status.selectTabKey === "18") {//完结
            this.gcStore.doSearch();
        } else if (this.status.selectTabKey === "2.1") {//汇报
            this.grStore.doSearch();
        } else if (this.status.selectTabKey === "2.2") {//汇报
            this.grStore.doSearch();
        }
    }

    /**
     * 弹窗动作执行方法
     * @param {*} id
     * @param {*} modelType
     */
    @action
    showModel(id, modelType) {
        //this.id = id;
        this.modelType = modelType;
        this.modelStore.handleShareDialog(true);
        this.modelStore.initModel(this.modelType, id);
        this.modelStore.form = new WeaForm();
    }

    /**
     * 弹窗动作执行方法
     * @param {*} id
     * @param {*} modelType
     */
    @action
    showconfirm(id, modelType, title, content) {
        this.modelType = modelType;
        let flag = false;
        if ((this.status.selectTabKey == '1' && this.status.roottask) || this.status.selectTabKey == '0') {//思维导图 删除根任务就关闭窗口 或者主页删除
            flag = true;
            this.modelStore.deletclose = flag;
        }
        this.modelStore.showConfirm({ id, modelType, title, content });

    }

    //右侧操作按钮方法
    @action
    doAction(key) {
        let taskId = this.MenuCurFlag ? this.skey : this.id;
        if (key == BtnType.BTN_SEARCH) {
            this.onListSearch();
        } else if (key === BtnType.BTN_SHARECREATE) {//添加共享
            this.shareStore.ShowMessage(true);
            this.shareStore.onShowAdd(true);
        } else if (key === BtnType.BTN_SHARECR) {//共享
            this.shareStore.onShowAdd(true);
        } else if (key === BtnType.BTN_CREATE) {
            this.showModel(taskId, key);
        } else if (key === BtnType.BTN_EDIT) {
            let id = this.giStore.tableStoreMyAttent.selectedRowKeys; //领导批示
            this.showModel(id, key);
        } else if (key === BtnType.BTN_DELETE) {
            let ids = this.giStore.tableStoreMyAttent.selectedRowKeys; //删除领导批示
            this.showconfirm(ids, key, BtnType.getTitle(key), BtnType.getContent(key));
        } else if (key === BtnType.BTN_DELETEBATCH) {//删除共享
            let ids = this.shareStore.tableStoreMyAttent.selectedRowKeys;
            this.modelStore.id = taskId;
            this.showconfirm(ids, key, BtnType.getTitle(key), BtnType.getContent(key));
        } else if (key === BtnType.BTN_TASKDELETE) {//删除任务
            this.modelStore.id = taskId;
            let title = getLabel('506748', '确认要删除该任务吗？')
            if (this.status.tasksize1 > 0 && this.status.selectTabKey === "0") {
                title = BtnType.getTitle(key)
            }
            if (this.status.tasksize2 > 0 && this.status.selectTabKey === "1") {
                title = BtnType.getTitle(key)
            }

            this.showconfirm(taskId, key, title, BtnType.getContent);
        } else if (key === BtnType.BTN_TASTREOPEN) {//重新开启
            let title = BtnType.getTitle(key)
            if (this.status.superStatus != 3) {//上级任务为已完成
                title = getLabel('508192', '确认要重新开启此任务吗？');
            }
            this.modelStore.id = taskId;

            this.showconfirm(taskId, key, title, BtnType.getContent);
        } else {
            if (BtnType.checkShowModel(key)) {//汇报
                this.getActionSetting(taskId, key);
            } else if (BtnType.checkShowConfirm(key)) {
                this.showconfirm(taskId, key, BtnType.getTitle(key), BtnType.getContent(key));
            } else {
                let _this = this;
                if (key === BtnType.BTN_RECEIVE) {
                    this.modelStore.signingTask(taskId, this.getCardBtnInfo, _this);
                } else if (key === BtnType.BTN_ATTENTION) {
                    this.modelStore.attentTask(taskId, this.getCardBtnInfo, _this);
                } else if (key === BtnType.BTN_DISATTENTION) {
                    this.modelStore.disAttentTask(taskId, this.getCardBtnInfo, _this);
                } else {
                    this.modelStore.subBtn(key, taskId)
                }
            }
        }

        if (!this.MenuCurFlag) {
            if (BtnType.isRefreshCardBtn(key)) {
                this.getCardBtnInfo(this);
            }
        }
    }

    @action
    setRightMenuCur(params = {}) {
        this.skey = params.skey;
        Apis.checkRightMenuBtn({ "taskid": params.skey }).then(data => {
            if (parseInt(data.status) == 1) {
                let keys = data.keyOrders || [];
                for (var i = 0; i < keys.length; i++) {
                        let orderKey = keys[i];
                        if (orderKey =="5" && data.flag0) { //反馈
                            params.rc.push({ key: "5", label: data.flag0title, icon: "icon-coms-Collection2" });
                        }
                        if (orderKey =="17" && data.flag5) {//延期
                            params.rc.push({ key: "17", label: data.flag5title, icon: "icon-coms-edit" });
                        }
                        if (orderKey =="18" && data.flag6) {//办结
                            params.rc.push({ key: "18", label: data.flag6title, icon: "icon-coms-Edition" });
                        }
                        if (orderKey =="13" && data.flag7) {//下发
                            params.rc.push({ key: "13", label: data.flag7title, icon: "icon-coms-Collection2" });
                        }
                        if (orderKey =="4" && data.flag2) {//分解
                            params.rc.push({ key: "4", label: data.flag2title, icon: "icon-coms-DisplaySubordinates" });
                        }
                        if (orderKey =="3" && data.flag1) {//变更
                            params.rc.push({ key: "3", label:data.flag1title, icon: "icon-coms-edit" });
                        }
                        if (orderKey =="8" && data.flag8) {//催办
                            params.rc.push({ key: "8", label: data.flag8title, icon: "icon-coms-Edition" });
                        }
                        if (orderKey =="6" && data.flag3) {//完成
                            params.rc.push({ key: "6", label: data.flag3title, icon: "icon-coms-Approval" });
                        }
                        if (orderKey =="7" && data.flag4) {//废弃
                            params.rc.push({ key: "7", label: data.flag4title, icon: "icon-coms-delete" });
                        }
                        if (orderKey =="1003" && data.flag9) {//删除
                            this.status.tasksize2 = data.tasksize - 1;
                            this.status.roottask = data.roottask;
                            params.rc.push({ key: "1003", label: data.flag9title, icon: "icon-coms-delete" });
                        }
                        if (orderKey =="1004" && data.flag10) {//重新开启
                            this.status.superStatus = data.superStatus;
                            params.rc.push({ key: "1004", label: data.flag10title, icon: "icon-coms-Synchro" });
                        }
                }
                this.rightMenuCur = params.rc;
                this.MenuCurFlag = true;
            } else {
                message.error(data.error);
            }
        }).catch(error => {
            message.error(error);
        });
    }

    @action
    clearRightMenuCur() {
        this.rightMenuCur = [];
        this.MenuCurFlag = false;
    }

    /**
     * 触发方式（0卡片触发、1触发流程）（key 0新建、1汇报、2分解、3催办、4变更）
     * @param {*} params
     */
    @action
    getActionSetting(taskId, key) {
        let _this = this;
        Apis.getActionSetting({ "taskid": taskId, "actiontype": key }).then(data => {
            if (parseInt(data.status) == 1) {
                if (data.isShowModel) {
                    _this.showModel(taskId, key);
                } else {
                    if (data.status == "-1") {
                        message.error(data.error);
                    } else {
                        window.open("/spa/workflow/index_form.jsp#/main/workflow/req?requestid=" + data.newrequestid + "&workflowid=" + data.flowid + "&taskid=" + taskId + "&actiontype=" + key);
                    }
                }
            } else {
                message.error(data.error);
            }
        }).catch(error => {
            message.error(error);
        });
    }
    @action
    getCardStorefun = (store) => {
        this.Card = store;
    }
}